package com.example.admin.brainwave.ui.activity;

import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.admin.brainwave.R;
import com.example.admin.brainwave.base.BaseActivity;
import com.example.admin.brainwave.bean.CurrencyBean;
import com.example.admin.brainwave.net.GetNoteTask;
import com.example.admin.brainwave.net.RegBySMSTask;
import com.example.admin.brainwave.utils.Const;
import com.example.admin.brainwave.utils.MyCountDownTimer;
import com.example.admin.brainwave.utils.ToastUtil;

import butterknife.BindView;

public class RegisterActivity extends BaseActivity {


    @BindView(R.id.iv_top_back)
    ImageView ivTopBack;
    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;
    @BindView(R.id.iv_reg_phone)
    ImageView ivRegPhone;
    @BindView(R.id.et_reg_phone)
    EditText etRegPhone;
    @BindView(R.id.ll_reg_phone)
    LinearLayout llRegPhone;
    @BindView(R.id.iv_reg_pass)
    ImageView ivRegPass;
    @BindView(R.id.et_reg_pass)
    EditText etRegPass;
    @BindView(R.id.ll_reg_pass)
    LinearLayout llRegPass;
    @BindView(R.id.iv_reg_validate)
    ImageView ivRegValidate;
    @BindView(R.id.et_reg_validate)
    EditText etRegValidate;
    @BindView(R.id.btn_reg_validate)
    Button btnRegValidate;
    @BindView(R.id.ll_reg_validate)
    LinearLayout llRegValidate;
    @BindView(R.id.btn_reg_reg)
    Button btnRegReg;

    private String phone;
    private String pass;
    private String code;
    MyCountDownTimer myCountDownTimer;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_register;
    }


    @Override
    protected void initView() {
        tvTopTitle.setText(getResources().getString(R.string.register));
    }


    @Override
    protected void initData() {
        myCountDownTimer = new MyCountDownTimer(60000, 1000, btnRegValidate, this);
    }

    @Override
    protected void addListener() {
        ivTopBack.setOnClickListener(view -> finish());
        btnRegValidate.setOnClickListener(view -> {
            phone = etRegPhone.getText().toString();
            if (!TextUtils.isEmpty(phone) && phone.length() == 11) {
                myCountDownTimer.start();
                getValidate();
            }
        });
        btnRegReg.setOnClickListener(view -> {
            if (isEmpty()) {
                register();
            }
        });
    }

    private void getValidate() {
        new GetNoteTask(this, o -> {
            if (o != null) {
                CurrencyBean c = (CurrencyBean) o;
                ToastUtil.showShort(RegisterActivity.this, c.getMessage());
            }
        }).execute(phone);
    }


    private void register() {
        new RegBySMSTask(this, o -> {
            if (o != null) {
                CurrencyBean c = (CurrencyBean) o;
                if (Const.OKCODE.equals(c.getCode())) {
                    ToastUtil.showShort(RegisterActivity.this, c.getMessage());
                    finish();
                } else {
                    ToastUtil.showShort(RegisterActivity.this, c.getMessage());
                }
            }
        }).execute(phone, pass, code);
    }

    private boolean isEmpty() {
        phone = etRegPhone.getText().toString();
        pass = etRegPass.getText().toString();
        code = etRegValidate.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            ToastUtil.showShort(RegisterActivity.this, getResources().getString(R.string.phone) + " " + getResources().getString(R.string.not_null));
            return false;
        }
        if (TextUtils.isEmpty(pass)) {
            ToastUtil.showShort(RegisterActivity.this, getResources().getString(R.string.password) + " " + getResources().getString(R.string.not_null));
            return false;
        }
        if (pass.length() < 8) {
            ToastUtil.showShort(this, getResources().getString(R.string.password) + " " + getResources().getString(R.string.length_too_short));
            return false;
        }
        if (TextUtils.isEmpty(code)) {
            ToastUtil.showShort(RegisterActivity.this, getResources().getString(R.string.verification_code) + " " + getResources().getString(R.string.not_null));
            return false;
        }
        return true;
    }

}
